home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 10868 / 10868.xpi / chrome / sync.jar / content / fx-weave-overlay.js < prev    next >
Text File  |  2010-02-02  |  5KB  |  151 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Bookmarks sync code.
  15.  *
  16.  * The Initial Developer of the Original Code is Mozilla.
  17.  * Portions created by the Initial Developer are Copyright (C) 2007
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *  Dan Mills <thunder@mozilla.com>
  22.  *  Chris Beard <cbeard@mozilla.com>
  23.  *  Dan Mosedale <dmose@mozilla.org>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. function FxWeaveGlue() {
  40. }
  41. FxWeaveGlue.prototype = {
  42.  
  43.   doInitTabsMenu: function FxWeaveGlue__doInitTabsMenu() {
  44.     // Don't do anything if the tabs engine isn't ready
  45.     if (!Weave.Engines.get("tabs"))
  46.       return;
  47.  
  48.     this._populateTabs();
  49.     this._refetchTabs();
  50.   },
  51.  
  52.   _refetchTabs: function _refetchTabs() {
  53.     // Don't bother refetching tabs if we already did so recently
  54.     let lastFetch = Weave.Svc.Prefs.get("lastTabFetch", 0);
  55.     let now = Math.floor(Date.now() / 1000);
  56.     if (now - lastFetch < 30)
  57.       return;
  58.  
  59.     // Asynchronously fetch the tabs
  60.     setTimeout(function() {
  61.       let engine = Weave.Engines.get("tabs");
  62.       let lastSync = engine.lastSync;
  63.  
  64.       // Force a sync only for the tabs engine
  65.       engine.lastModified = null;
  66.       engine.sync();
  67.       Weave.Svc.Prefs.set("lastTabFetch", now);
  68.  
  69.       // XXX Can't seem to force the menu to redraw ? :(
  70.     }, 0);
  71.   },
  72.  
  73.   _populateTabs: function _populateTabs() {
  74.     // Lazily add the listener to show the selected item's uri
  75.     let menu = document.getElementById("sync-tabs-menu");
  76.     if (!menu.hasStatusListener) {
  77.       menu.hasStatusListener = true;
  78.  
  79.       menu.addEventListener("DOMMenuItemActive", function(event) {
  80.         XULBrowserWindow.setOverLink(event.target.weaveUrls.toString());
  81.       }, false);
  82.       menu.addEventListener("DOMMenuItemInactive", function() {
  83.         XULBrowserWindow.setOverLink("");
  84.       }, false);
  85.     }
  86.  
  87.     // Clear out old menu contents
  88.     while (menu.itemCount > 1)
  89.       menu.removeItemAt(menu.itemCount - 1);
  90.  
  91.     let engine = Weave.Engines.get("tabs");
  92.     let localTabs = engine._store.getAllTabs();
  93.     for (let [guid, client] in Iterator(engine.getAllClients())) {
  94.       // Remember if we need to append the client name
  95.       let appendClient = true;
  96.       let pageUrls = [];
  97.  
  98.       client.tabs.forEach(function({title, urlHistory, icon}) {
  99.         // Skip tabs that are already open
  100.         let pageUrl = urlHistory[0];
  101.         if (localTabs.some(function(tab) tab.urlHistory[0] == pageUrl))
  102.           return;
  103.  
  104.         // Add the client once and point it to the array of urls to open
  105.         if (appendClient) {
  106.           appendClient = false;
  107.           let item = menu.appendItem(client.clientName);
  108.           item.className = "menuitem-iconic";
  109.           item.style.listStyleImage = "url(chrome://weave/skin/tab.png)";
  110.           item.weaveUrls = pageUrls;
  111.         }
  112.  
  113.         let iconUrl = Weave.Utils.getIcon(icon, "chrome://weave/skin/tab.png");
  114.         title = title == "" ? pageUrl : title;
  115.  
  116.         // Add a menuitem that knows what url to open
  117.         let item = menu.appendItem("   " + title);
  118.         item.className = "menuitem-iconic";
  119.         item.style.listStyleImage = "url(" + iconUrl + ")";
  120.         item.weaveUrls = [pageUrl];
  121.  
  122.         // Add this to the list of urls to open for the client name
  123.         pageUrls.push(pageUrl);
  124.       });
  125.     }
  126.  
  127.     // Show/hide the "no tabs available" if necessary
  128.     document.getElementById("sync-no-tabs-menu-item").hidden =
  129.       (menu.itemCount > 1);
  130.   },
  131.  
  132.   onCommandTabsMenu: function FxWeaveGlue_onCommandTabsMenu(event) {
  133.     // Open each url in its own tab
  134.     let lastTab;
  135.     event.target.weaveUrls.forEach(function(url) {
  136.       lastTab = openUILinkIn(url, "tab")
  137.     });
  138.  
  139.     // Switch to the last opened tab
  140.     gBrowser.selectedTab = lastTab;
  141.   },
  142.  
  143.   shutdown: function FxWeaveGlue__shutdown() {
  144.   }
  145. }
  146.  
  147. let gFxWeaveGlue;
  148.  
  149. window.addEventListener("load", function(e) { gFxWeaveGlue = new FxWeaveGlue(); }, false);
  150. window.addEventListener("unload", function (e) { gFxWeaveGlue.shutdown(e); }, false);
  151.